import $ from '../common'

var app = getApp()

Page({
	data: {},
	bindKeyInput(e) {
		let val = e.detail.value
		this.data.value = val
		this.setData({
			num: val.length + "/500"
		})
	},
	sure() {
		let data = this.data
		let module = $.parentData()["$ET_data"];
		!module && [$.parentData().$ET_data = {}, module = {}]
		if(!$.isBlankSpace(data.value)) {
			module.before && module.before()
		}
		const setData = (e) => {
			let module = $.parentData()["$ET_data"]
			module[data.key] = data.value
			e && [module["last_cbData"] = e]
			e && [module[data.key + "_cbData"] = e]
			wx.hideToast()
			wx.navigateBack()
		}
		if(data.required == "true" && !data.value){
			$.tip("请输入内容后再操作哦~")
			return false
		}
		if(data.api) {
			let param = JSON.parse(unescape(data.param))
			$.assign(param, {
				[data.key]: data.value || " "
			})
			$.post(data.api, param, (e = {}) => {
				setData(e)
        wx.showToast({
          title: '修改成功！',
        })
			})
		} else {
			setData((!$.isBlankSpace(data.value)) ? {
				key: data.key,
				value: data.value
			} : "")
		}
	},
	onLoad(e) {
		this.data = $.parentData()._$editText || {}
		wx.setNavigationBarTitle({
			title: this.data.title
		})
		let module = $.parentData()["$ET_data"];
		!module && [$.parentData().$ET_data = {}, module = {}]
		module.last_cbData = ""
		this.setData({
			data: this.data,
			num: this.data.value.length + "/500"
		})
	}
})